<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>对象的扩展运算符</h1>
    <ul>
        <li>完成对象的浅拷贝：引用数据类型仅拷贝引用地址（被引用对象的值修改之后，也会跟着修改）</li>、
        <li>完成对象的混入。</li>
    </ul>
    <p>目前唯一的深拷贝方法？先将对象转为json字符串，再把json字符串转回对象。</p>
    <script>
        let obj = {name: "奉先",age: 35};
        // 对象的...扩展，可以完成对对象的“浅拷贝”
        
        let obj2 = {...obj};
        console.log("obj2 = ",obj2);

        let friend = {
            name: "罗佰君",
            age: 20
        }
        // 将friend赋值给obj的friend属性
        obj.friend = friend;

        let obj3 = {...obj};
        console.log("obj3 = ",obj3);

        friend.name = "胡正";
        console.log("obj3 = ",obj3);

        // 深拷贝
        let obj4 = {};
        let jsonStr = JSON.stringify(obj3.friend);
        obj4.friend = JSON.parse(jsonStr);

        // 混入
        let baseObj = {
            name: "匿名",
            sex: "保密",
            age: 18
        }

        let newObj = {
            ...baseObj,
            age: 20,
        }

        console.log("newObj = ",newObj);


    </script>
</body>
</html>